<?php
namespace Modules\Product\Entities;

use Modules\Core\Entities\BaseEntity as Model;
use ThrowException;

// use Modules\Like\Traits\Likeable;
// use Modules\Comment\Traits\Commentable;
use Modules\Core\Traits\Statable;
use Modules\Category\Traits\Categoryable;
use Modules\User\Traits\GroupPriceable;

class Product extends Model
{
    use Categoryable;
    use Statable;
    use GroupPriceable;
    // use Likeable;
    // use Commentable;
    
    protected $guarded = [];
    protected $table = 'products';

    protected $casts = [
        'tags' => 'array',
        'order_reward' => 'json',
        'category_ids' => 'array'
    ];

    public function scopeSearch($query, $key)
    {
        $query->where('number', 'like', '%' . $key . '%');

        if (env('PRODUCT_MATCH_TYPE') === 'search_key') {
            $query->orWhere('search_key', 'like', '%' . $key . '%');
        }
        else {
            $query->orWhere('title', 'like', '%' . $key . '%');
        }

        return $query;
    }

    public function scopeOnlyScorePrice($query) 
    {
        return $query->whereNull('money_price')->orWhere('money_price', 0);
    }

    public function scopeNumber($query, $number)
    {
        return $query->where('number',  $number)->orWhereHasIn('skus', function ($q) use ($number) {
            return $q->where('number', $number);
        });
    }

    // 在小程序上显示
    public function displayOnMiniapp($qeury)
    {
        return $query;
    }

    public function scopeGroupon($query, $flag)
    {
        return $query->whereHas('groupon');
    }

    public function scopeStatus($query, $status) 
    {
        if ($status === 'on_stock') {
            return $query->onStock();
        }
        elseif ($status === 'off_stock') {
            return $query->offStock();
        }
        elseif ($status === 'sell_out') {
            return $query->where('stock', '<=', 0);
        }
        elseif ($status === 'marked') {
            return $query->whereNotNull('marked_at');
        }
    }
   
    public function getNodeEntity()
    {
        return $this;
    }

    // 拼团
    public function groupon()
    {
        return $this->hasOne('\Modules\Groupon\Entities\Groupon', 'product_id')->valid();
    }

    // 角标
    public function image_tag()
    {
        return $this->belongsTo('\Modules\Category\Entities\ImageTag', 'image_tag_id');
    }

    // sku
    public function skus()
    {
        return $this->hasMany('\Modules\Product\Entities\Sku', 'product_id');
    }

    // 扩展信息
    public function ext()
    {
        return $this->hasOne('\Modules\Product\Entities\ProductExt');
    }

    public function updateSalesAndStock()
    {
        $this->update([
            'sales' => $this->skus()->sum('sales'),
            'stock' => $this->skus()->sum('stock'),
            'cover_chip_status' => $this->skus()->sum('cover_chip_status') > 0 ? 1 : 0,
        ]);
    }

    static public function beforeGetList($options)
    {
        if (isset($options['brand_id']))
            $options['where']['brand_id'] = $options['brand_id'];

        if (isset($options['status'])) {
            $options['scopes']['status'] = $options['status'];
            // $options['where']['status'] = $options['status'];
        }

        if (@$options['category1_id']) {
            $options['where']['category1_id'] = $options['category1_id'];
        }

        if (@$options['category2_id']) {
            $options['where']['category2_id'] = $options['category2_id'];
        }

        if (@$options['number']) {
            $options['scopes']['number'] = $options['number'];
        }

        if (isset($options['is_groupon']))
            $options['scopes']['groupon'] = $options['is_groupon'] ? true : false;

        return $options;
    }

    public function isOnOffer()
    {
        return $this->status === 1;
    }
}
